/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frame1.java
 *
 * Created on 17-mar-2010, 16.40.06
 */
package frame;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JLabel;

/**
 *
 * @author paolo
 */
public class frame1 extends javax.swing.JFrame {

    private class auto extends Thread{
        JLabel auto;

        public auto(JLabel car){
            this.auto=car;
            this.auto.setText("Macchina");
        }

        @Override
        public void run() {
                int x=this.auto.getLocation().x;
                int y=this.auto.getLocation().y;
            for(int i=0; i<300; i++){
                this.auto.setLocation(x, y+i);
                try{
                    sleep(10);
                }catch(InterruptedException e){

                }
            }
            this.auto.setLocation(x, 0);
        }

    }

    private class movimento extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            int x = jLabel1.getLocation().x;
            int y = jLabel1.getLocation().y;
            switch (e.getKeyCode()) {
                case KeyEvent.VK_LEFT:
                    if(x-10>0){
                    jLabel1.setLocation(x - 10, y);
                    }
                    break;
                case KeyEvent.VK_RIGHT:
                    if(x+10<400-jLabel1.getSize().width){
                    jLabel1.setLocation(x + 10, y);
                    }
                    break;
            }
        }
    }

    /** Creates new form frame1 */
    public frame1() {
        initComponents();
        this.setFocusable(true);
        addKeyListener(new movimento());
        new auto(this.jLabel2).start();
        new auto(this.jLabel3).start();
        new auto(this.jLabel4).start();
        new auto(this.jLabel5).start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Car");
        jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel2.setText("jLabel2");

        jLabel3.setText("jLabel3");

        jLabel4.setText("jLabel4");

        jLabel5.setText("jLabel5");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                        .addComponent(jLabel4)
                        .addGap(55, 55, 55)
                        .addComponent(jLabel3)
                        .addGap(46, 46, 46)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(353, Short.MAX_VALUE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 219, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frame1().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    // End of variables declaration//GEN-END:variables
}
